package com.kws.merchant.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kws.merchant.domain.ShopCartEntity;
import com.kws.merchant.domain.vo.ShopCartVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author : zhangbenting
 * @date : 2024/10/31 20:07
 * @Version: 1.0
 */
public interface ShopCartMapper extends BaseMapper<ShopCartEntity> {

    @Select("SELECT " +
            " a.id, " +
            " a.goods_count, " +
            " b.id AS goods_id, " +
            " b.`name`, " +
            " b.price, " +
            " b.img, " +
            " c.id AS merchant_id, " +
            " c.company_name as shops_name, " +
            " d.id AS specification_id, " +
            " d.color, " +
            " d.specification_model, " +
            " d.img AS specification_img  " +
            "FROM " +
            " `tb_shop_cart` AS a " +
            " LEFT JOIN `tb_device` AS b ON a.device_id = b.id " +
            " LEFT JOIN `tb_merchant` AS c ON b.merchant_id = c.id " +
            " LEFT JOIN `tb_specification` AS d ON d.goods_id = b.id  " +
            " WHERE " +
            " a.type = 0  " +
            " AND a.user_id = ${userId}  " +
            " ORDER BY " +
            " a.create_time DESC" +
            " limit ${pageSize},${totalPage} ")
    List<ShopCartVo> getDeviceShopCartList(@Param("userId") Long userId,
                                           @Param("pageSize") Integer pageSize,
                                           @Param("totalPage") Integer totalPage);

    @Select("SELECT " +
            " a.id, " +
            " a.goods_count, " +
            " b.id AS goods_id, " +
            " b.`name`, " +
            " b.price, " +
            " b.img, " +
            " c.id AS merchant_id, " +
            " c.company_name as shops_name" +
            " FROM " +
            " `tb_shop_cart` AS a " +
            " LEFT JOIN `tb_serve` AS b ON a.device_id = b.id " +
            " LEFT JOIN `tb_merchant` AS c ON b.merchant_id = c.id  " +
            "WHERE " +
            " a.type = 1 " +
            " AND a.user_id = ${userId}  " +
            " ORDER BY " +
            " a.create_time DESC "+
            " limit ${pageSize},${totalPage} "
    )
    List<ShopCartVo> getServeShopCartList(@Param("userId") Long userId,
                                          @Param("pageSize") Integer pageSize,
                                          @Param("totalPage") Integer totalPage);

    /**
     * 购物车加购人数
     *
     * @param userId
     * @param time
     * @return
     */
    @Select("select  count(DISTINCT user_id) from tb_shop_cart where merchant_id='${userId}' and DATE(create_time) like '${time}';")
    Double merchantShopCartByUserAndTime(@Param("userId") Long userId, @Param("time") String time);

    /**
     * 总加购人数
     * @param userId
     */
    @Select("select  count(DISTINCT user_id) from tb_shop_cart where merchant_id='${userId}';")
    Double countShopCartByUser(Long userId);
}
